package com.liuzh.deviceinfo.location;

import android.hardware.GeomagneticField;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.GnssStatus.Callback;
import android.location.GpsStatus;
import android.location.GpsStatus.Listener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Toast;
import c.h.a.c0.c;
import c.h.a.c0.d;
import c.h.a.c0.e;
import c.h.a.e0.a;
import c.h.a.m0.h;
import com.liuzh.deviceinfo.base.BaseActivity;
import com.liuzh.deviceinfo.view.GpsSkyView;

public class RealtimeSatelliteActivity extends BaseActivity implements SensorEventListener {
    public static final /* synthetic */ int G = 0;
    public boolean A = false;
    public float[] B = new float[16];
    public float[] C = new float[4];
    public float[] D = new float[16];
    public float[] E = new float[3];
    public GeomagneticField F;
    public LocationManager q;
    public Callback r;
    public Listener s;
    public GpsStatus t;
    public LocationListener u;
    public GpsSkyView v;
    public SensorManager w;
    public Location x;
    public Sensor y;
    public Sensor z;

    public void onAccuracyChanged(Sensor sensor, int i) {
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (a.b(this, "android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION")) {
            LocationProvider provider;
            setContentView(2131492897);
            z();
            this.v = (GpsSkyView) findViewById(2131296964);
            AsyncTask.execute(new c(this, (ViewGroup) findViewById(2131296478)));
            String str = "sensor";
            this.w = (SensorManager) getSystemService(str);
            SensorManager sensorManager = (SensorManager) getSystemService(str);
            Object obj = (sensorManager == null || sensorManager.getDefaultSensor(11) == null) ? null : 1;
            Sensor defaultSensor;
            if (obj != null) {
                defaultSensor = this.w.getDefaultSensor(11);
                this.y = defaultSensor;
                this.w.registerListener(this, defaultSensor, 16000);
            } else {
                defaultSensor = this.w.getDefaultSensor(3);
                this.z = defaultSensor;
                if (defaultSensor != null) {
                    this.w.registerListener(this, defaultSensor, 1);
                }
            }
            LocationManager locationManager = (LocationManager) getSystemService("location");
            this.q = locationManager;
            LocationProvider locationProvider = null;
            if (locationManager != null) {
                locationProvider = locationManager.getProvider("gps");
                provider = this.q.getProvider("network");
            } else {
                provider = null;
            }
            LocationManager locationManager2 = this.q;
            if (locationManager2 == null || locationProvider == null) {
                finish();
                Toast.makeText(this, getString(2131689951), 0).show();
            } else {
                this.u = new d(this);
                locationManager2.requestLocationUpdates(locationProvider.getName(), 1000, 1.0f, this.u);
                if (provider != null) {
                    this.q.requestLocationUpdates(provider.getName(), 1000, 1.0f, this.u);
                }
                if (h.d) {
                    e eVar = new e(this);
                    this.r = eVar;
                    this.q.registerGnssStatusCallback(eVar);
                } else {
                    c.h.a.c0.a aVar = new c.h.a.c0.a(this);
                    this.s = aVar;
                    this.q.addGpsStatusListener(aVar);
                }
            }
            return;
        }
        Toast.makeText(this, 2131690076, 0).show();
        finish();
    }

    public void onDestroy() {
        super.onDestroy();
        LocationManager locationManager = this.q;
        if (locationManager != null) {
            LocationListener locationListener = this.u;
            if (locationListener != null) {
                locationManager.removeUpdates(locationListener);
            }
            if (h.d) {
                Callback callback = this.r;
                if (callback != null) {
                    this.q.unregisterGnssStatusCallback(callback);
                }
            } else {
                Listener listener = this.s;
                if (listener != null) {
                    this.q.removeGpsStatusListener(listener);
                }
            }
        }
        SensorManager sensorManager = this.w;
        if (sensorManager != null) {
            Sensor sensor = this.y;
            if (sensor != null) {
                sensorManager.unregisterListener(this, sensor);
                return;
            }
            sensor = this.z;
            if (sensor != null) {
                sensorManager.unregisterListener(this, sensor);
            }
        }
    }

    public void onSensorChanged(SensorEvent sensorEvent) {
        double d;
        int type = sensorEvent.sensor.getType();
        if (type == 3) {
            d = (double) sensorEvent.values[0];
        } else if (type == 11) {
            if (this.A) {
                System.arraycopy(sensorEvent.values, 0, this.C, 0, 4);
                SensorManager.getRotationMatrixFromVector(this.B, this.C);
            } else {
                try {
                    SensorManager.getRotationMatrixFromVector(this.B, sensorEvent.values);
                } catch (IllegalArgumentException unused) {
                    this.A = true;
                    System.arraycopy(sensorEvent.values, 0, this.C, 0, 4);
                    SensorManager.getRotationMatrixFromVector(this.B, this.C);
                }
            }
            int rotation = getWindowManager().getDefaultDisplay().getRotation();
            if (rotation == 0) {
                SensorManager.getOrientation(this.B, this.E);
            } else if (rotation == 1) {
                SensorManager.remapCoordinateSystem(this.B, 2, 129, this.D);
                SensorManager.getOrientation(this.D, this.E);
            } else if (rotation == 2) {
                SensorManager.remapCoordinateSystem(this.B, 129, 130, this.D);
                SensorManager.getOrientation(this.D, this.E);
            } else if (rotation != 3) {
                SensorManager.getOrientation(this.B, this.E);
            } else {
                SensorManager.remapCoordinateSystem(this.B, 130, 1, this.D);
                SensorManager.getOrientation(this.D, this.E);
            }
            d = Math.toDegrees((double) this.E[0]);
            Math.toDegrees((double) this.E[1]);
        } else {
            return;
        }
        GeomagneticField geomagneticField = this.F;
        if (geomagneticField != null) {
            d = (double) (((((float) (d + ((double) geomagneticField.getDeclination()))) % 360.0f) + 360.0f) % 360.0f);
        }
        this.v.c(d);
    }
}
